package com.bayss.web.controller;

import com.bayss.core.DokoServerException;
import com.bayss.core.DokoServerExceptionFactor;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.found.ZaihuMessage;
import com.bayss.core.entity.found.ZaihuMessageVo;
import com.bayss.core.entity.user.UserInfo;
import com.bayss.core.enums.MessageStatus;
import com.bayss.core.service.FoundService;
import com.bayss.core.service.user.UserService;
import com.bayss.web.uresponse.UserResponse;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
public class FoundController extends ApiBaseController {

    @Autowired
    private FoundService foundService;
    @Autowired
    private UserService userService;


    /**
     * 留言列表
     */
    @RequestMapping(value = "/front/found/foundListSearchTitle.json")
    public ResultMsg foundListSearchTitle(HttpServletRequest httpServletRequest, Integer orderType, String titleName, Integer page, Integer pageSize) {
        String openid = httpServletRequest.getHeader("openid");
        UserInfo userInfo=null;
        if (StringUtils.isNotBlank(openid)) {
             userInfo = userService.getUser(openid);
        }

        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setPage(page);
        vo.setSize(pageSize);
        vo.setTitleName(titleName);
        vo.setOrderType(orderType);
        vo.setStatus(MessageStatus.checked.getVal());
        PageInfo<ZaihuMessage> list = foundService.foundlistSearchTitleByPage(userInfo,vo);
        return ResultMsg.success(list);
    }

    /**
     * 增加留言
     */
    @RequestMapping(value = "/front/found/addFoundMessage.json", method = RequestMethod.POST)
    public ResultMsg addFoundMessage(UserResponse userResponse, String titleName, String content) {
        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setTitleName(titleName);
        vo.setContent(content);
        vo.setUserId(userResponse.getUid().intValue());
        return foundService.addfoundMessage(vo);
    }


    /**
     * 查看留言详情
     */
    @RequestMapping(value = "/front/found/findByFoundMessageId.json", method = RequestMethod.GET)
    public ResultMsg findByFoundMessageId(HttpServletRequest httpServletRequest, Integer id) {
        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setId(id);
        vo.setMessageId(id);

        String openid = httpServletRequest.getHeader("openid");
        UserInfo userInfo=null;
        if (StringUtils.isNotBlank(openid)) {
            userInfo = userService.getUser(openid);
            vo.setUserId(userInfo.getId().intValue());
        }
        ResultMsg restresult = foundService.findByfoundMessageId(vo);
        return restresult;
    }


    /**
     * 评论留言
     */
    @RequestMapping(value = "/front/found/commentsMessageForUser.json", method = RequestMethod.POST)
    public ResultMsg commentsMessageForUser(UserResponse userResponse, Integer messageId, String comContent) {
        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setUserId(userResponse.getUid().intValue());
        vo.setMessageId(messageId);
        vo.setComContent(comContent);
        ResultMsg restresult = foundService.commentsMessageforUser(userResponse.getUserInfo(), vo);
        return restresult;
    }

    /**
     * 点赞留言
     */
    @RequestMapping(value = "/front/found/likeMessageForUser.json", method = RequestMethod.POST)
    public ResultMsg likeMessageForUser(UserResponse userResponse, Integer messageId) {
        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setUserId(userResponse.getUid().intValue());
        vo.setMessageId(messageId);
        ResultMsg restresult = foundService.likeMessageforUser(vo);
        return restresult;
    }

    /**
     * 评论列表
     */
    @RequestMapping(value = "/front/found/commentsList.json")
    public ResultMsg commentsList(Integer messageId, Integer page, Integer pageSize) {
        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setPage(page);
        vo.setSize(pageSize);
        vo.setMessageId(messageId);
        vo.setStatus(MessageStatus.checked.getVal());
        return foundService.commentslist(vo);
    }

    /**
     * 点赞列表
     */
    @RequestMapping(value = "/front/found/likesList.json")
    public ResultMsg likesList(Integer messageId, Integer page, Integer pageSize) {
        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setPage(page);
        vo.setSize(pageSize);
        vo.setMessageId(messageId);
        ResultMsg restresult = foundService.likeslist(vo);
        return restresult;
    }


    /**
     * 删除我的评论
     */
    @RequestMapping(value = "/front/found/deleteByComentId.json")
    public ResultMsg deleteByComentId(Integer id) {
        return foundService.deleteByComentId(id);
    }

    /**
     * 删除我的留言
     */
    @RequestMapping(value = "/front/found/deleteByMessageId.json")
    public ResultMsg deleteByMessageId(Integer id) {
        return foundService.deleteByMessageId(id);
    }

    /**
     * 取消点赞
     */
    @RequestMapping(value = "/front/found/cancelLike.json", method = RequestMethod.POST)
    public ResultMsg cancelLike(UserResponse userResponse, Integer messageId) {
        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setUserId(userResponse.getUid().intValue());
        vo.setMessageId(messageId);
        ResultMsg restresult = foundService.cancelLike(vo);
        return restresult;
    }

    /**
     * 我的评论列表
     */
    @RequestMapping(value = "/front/found/commentMyList.json", method = RequestMethod.POST)
    public ResultMsg commentMyList(UserResponse userResponse, Integer page, Integer pageSize) {
        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setPage(page);
        vo.setSize(pageSize);
        vo.setUserId(userResponse.getUid().intValue());
        return foundService.commentMylist(vo);
    }

    /**
     * 我的留言列表
     */
    @RequestMapping(value = "/front/found/messgeMyList.json")
    public ResultMsg messgeMyList(UserResponse userResponse, Integer page, Integer pageSize) {
        ZaihuMessageVo vo = new ZaihuMessageVo();
        vo.setPage(page);
        vo.setSize(pageSize);
        vo.setUserId(userResponse.getUid().intValue());
        PageInfo<ZaihuMessage> list = foundService.foundlistSearchTitleByPage(userResponse.getUserInfo(),vo);
        return ResultMsg.success(list);
    }

}
